package yun.service;

import java.util.ArrayList;
import java.util.List;

/**
 * Factory Pattern
 * 
 * @see yun.service.SequencePlaylist
 * @see yun.service.SequenceRepeatPlaylist
 * @see yun.service.ShufflePlaylist
 * @see yun.service.ShuffleRepeatPlaylist
 */
public final class PlaylistFactory {

	/**
	 * Create playlist
	 * 
	 * @param index
	 * @param list
	 * @param isRepeat
	 * @param isShuffle
	 * @return playlist
	 */
	public static Playlist<Long> createPlaylist(int index, long[] array, boolean isRepeat, boolean isShuffle) {
		ArrayList<Long> list = new ArrayList<Long>();
		for (long l : array) {
			list.add(l);
		}
		return createPlaylist(index, list, isRepeat, isShuffle);
	}

	/**
	 * Create playlist
	 * 
	 * @param index
	 * @param list
	 * @param isRepeat
	 * @param isShuffle
	 * @return playlist
	 */
	public static Playlist<Long> createPlaylist(int index, List<Long> list, boolean isRepeat, boolean isShuffle) {
		Playlist<Long> iterator = null;

		if (isRepeat) {
			if (isShuffle) {
				iterator = new ShuffleRepeatPlaylist<Long>(index, list);
			} else {
				iterator = new SequenceRepeatPlaylist<Long>(index, list);
			}
		} else {
			if (isShuffle) {
				iterator = new ShufflePlaylist<Long>(index, list);
			} else {
				iterator = new SequencePlaylist<Long>(index, list);
			}
		}
		return iterator;
	}


	/**
	 * Create playlist
	 * 
	 * @param index
	 * @param list
	 * @param isRepeat
	 * @param isShuffle
	 * @return playlist
	 */
	public static Playlist<Long> createShufflePlaylist(long[] array, boolean isRepeat) {
		ArrayList<Long> list = new ArrayList<Long>();
		for (long l : array) {
			list.add(l);
		}
		return createShufflePlaylist(list, isRepeat);
	}
	
	/**
	 * Create playlist
	 * 
	 * @param index
	 * @param list
	 * @param isRepeat
	 * @param isShuffle
	 * @return playlist
	 */
	public static Playlist<Long> createShufflePlaylist(List<Long> list, boolean isRepeat) {
		if (list == null || list.size() == 0) {
			throw new IllegalArgumentException("list is empty");
		}

		Playlist<Long> iterator = null;
		if (isRepeat) {
			iterator = new ShuffleRepeatPlaylist<Long>(0, list);
		} else {
			iterator = new ShufflePlaylist<Long>(0, list);
		}
		return iterator;
	}

	/**
	 * Create playlist
	 * 
	 * @param playlist
	 * @param isRepeat
	 * @param isShuffle
	 * @return playlist
	 */
	public static Playlist<Long> createPlaylist(Playlist<Long> playlist, boolean isRepeat, boolean isShuffle) {
		int index = playlist.getIndex();
		List<Long> list = playlist.getList();
		return createPlaylist(index, list, isRepeat, isShuffle);
	}
}
